package com.cater.project.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.project.entity.CompanyInvoice;
import com.cater.project.entity.DemandEnterprise;
import com.cater.project.entity.ProjectCollectionRecord;
import com.cater.project.service.CompanyInvoiceServiceImpl;
import com.cater.project.service.DemandEnterpriseServiceImpl;
import com.cater.project.service.ProjectCollectionRecordServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.talents.entity.Resume;
import com.cater.talents.service.ResumeServiceImpl;

/**
 * 描述：发票管理控制层
 * 
 * @author yong
 * @date 2019/04/25
 */
@Controller
@RequestMapping("/invoice")
public class CompanyInvoiceController {

	@Autowired
	private CompanyInvoiceServiceImpl service;
	@Autowired
	private DemandEnterpriseServiceImpl demandEnterpriseService;
	@Autowired
	private ResumeServiceImpl resumeService;
	@Autowired
	private ProjectCollectionRecordServiceImpl collectRecordService;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

	/**
	 * @发票管理管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "project/invoice/list";
	}

	/**
	 * @发票管理管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询发票管理分页数据")
	public PageResult<CompanyInvoice> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<CompanyInvoice> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("invoice:add")
	public String addView(CommonQuery query, Model model) {
		String demandId = query.getDemandId();
		String talentsId = query.getTalentsId();
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(talentsId)) {
			Resume resume = resumeService.getById(talentsId);
			if (resume != null) {
				model.addAttribute("resume", resume);
			}
		}
		if (StringUtils.isNotEmpty(demandId)) {
			DemandEnterprise enterprise = demandEnterpriseService.getById(demandId);
			List<CompanyInvoice> invoices = null;
			CompanyInvoice invoice = null;
			if (enterprise != null) {
				model.addAttribute("enterprise", enterprise);
				String companyId = enterprise.getCompanyId();
				if (StringUtils.isNotEmpty(companyId)) {
					query.setCompanyId(companyId);
					invoices = new ArrayList<CompanyInvoice>();
					invoices = service.getListBy(query);
					if (invoices.size() > 0) {
						invoice = new CompanyInvoice();
						invoice = invoices.get(0);
					} else {
						invoices = new ArrayList<CompanyInvoice>();
						query.setCommunicateId(null);
						invoices = service.getListBy(query);
						if (invoices.size() > 0) {
							invoice = new CompanyInvoice();
							invoice = invoices.get(0);
							invoice.setId(null);
							invoice.setStatus(null);
							invoice.setInvoiceNo(null);
							invoice.setExpressCompany(null);
							invoice.setExpressNumber(null);
						}
					}
					model.addAttribute("invoice", invoice);
				}
			}
		}
		if (StringUtils.isNotEmpty(communicateId)) {
			model.addAttribute("communicateId", communicateId);
		}
		return "project/invoice/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("invoice:add")
	@ResponseBody
	@SystemControllerLog(description = "添加发票管理")
	public JSONObject add(CompanyInvoice t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("invoice:edit")
	public String editView(String id, Model model, CommonQuery query) {
		CompanyInvoice t = service.getById(id);
		model.addAttribute("invoice", t);
		return "project/invoice/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("invoice:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改发票管理")
	public JSONObject update(CompanyInvoice t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			CompanyInvoice invoice = service.getById(t.getId());
			ProjectCollectionRecord collectionRecord = new ProjectCollectionRecord();
			if (invoice != null) {
				collectionRecord.setCandidate(invoice.getCandidate());
				collectionRecord.setTalentsId(invoice.getTalentsId());
				collectionRecord.setType(invoice.getType());
				collectionRecord.setStatus(invoice.getStatus());
				collectionRecord.setInvoiceNum(invoice.getInvoiceNo());
				collectionRecord.setAmountReceivable(invoice.getAccount());
				collectionRecord.setAccountStatus(1);
				collectionRecord.setAccountNo("SK" + sdf.format(new Date()));
				collectRecordService.saves(collectionRecord);
			}
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("invoice:delete")
	@SystemControllerLog(description = "删除发票管理")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("invoice:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		CompanyInvoice t = service.getById(id);
		model.addAttribute("invoice", t);
		return "project/invoice/detail";
	}

}